//
//  MyScene.m
//  Legions
//
//  Created by Legions on 02/07/14.
//  Copyright (c) 2014 RefeSoft. All rights reserved.
//

#import "MyScene.h"
#import "GameObjectType.h"
#import "Soldier.h"
#import "ArmyHandler.h"

SKNode                          *worldNode;
NSMutableArray                  *armyHandlesList;
ArmyHandler                     *handler0,*handler1;

UIBezierPath                    *circle;

@implementation MyScene{
    ArmyHandler *_movingArmyHandler;
    NSTimeInterval _lastUpdateTime;
    NSTimeInterval _dt;
}

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        
        self.backgroundColor        = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
        self.physicsWorld.gravity   = CGVectorMake(0, 0);
        
        worldNode                   = [SKNode node];
        worldNode.position          = CGPointMake(0, 0);
        [self addChild:worldNode];
        
        armyHandlesList              = [[NSMutableArray alloc] init];
        
        handler0 = [self createArmy : CGPointMake(0,0)];//CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
        handler1 = [self createArmy : CGPointMake(100,100)];


    }
    
    return self;
}

-(ArmyHandler*) createArmy : (CGPoint) startPosition{
    ArmyHandler                     *armyHandle     =   [[ArmyHandler alloc] initWithParentNode : worldNode position: startPosition];
    [armyHandlesList addObject:armyHandle];
    return armyHandle;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */
    
    
    CGPoint touchPoint = [[touches anyObject] locationInNode:self.scene];
    SKNode *node = [self nodeAtPoint:touchPoint];
    
    if([node.name isEqualToString:@"handler"]) {
       [(ArmyHandler *)node addPointToMove:touchPoint];
       _movingArmyHandler = (ArmyHandler *)node;
    }
    
/*    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        
        //follow path test
        circle = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 100) cornerRadius:100];
        SKShapeNode *shapeNode = [SKShapeNode node];
        shapeNode.path = circle.CGPath;
        [self addChild:shapeNode];
        SKAction *followCircle = [SKAction followPath:circle.CGPath asOffset:YES orientToPath:YES duration:5.0];
        [handler0 runAction:followCircle];
        
        //move here
        [handler1 moveTo:location duration:2];

    }
*/
 }

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint touchPoint = [[touches anyObject] locationInNode:self.scene];
    if(_movingArmyHandler) {
        [_movingArmyHandler addPointToMove:touchPoint];
        
    }
}


-(void)update:(CFTimeInterval)currentTime {
    _dt = currentTime - _lastUpdateTime;
    _lastUpdateTime = currentTime;
    
    
    [worldNode enumerateChildNodesWithName:@"handler" usingBlock:^(SKNode *handler, BOOL *stop) {

        [(ArmyHandler *)handler move:@(_dt)];
    }];
    
    
}

@end
